<template>
	<view>
		<view class="page" :style="'background-image: url(' + $c.config.media + 'yuemini/reg_bg.png);'">
			<view class="top" :style="`padding-top:`+heigh + `px`">
				<view class="top-icon" @click="back">
					<image class="img" :src="$c.config.imageIP + 'yuemini/ic_back.png'" mode="aspectFill"></image>
				</view>
				<view class="top-title">{{title}}</view>
			</view>
			<view :style="{height:heigh + 'px'}">
			</view>
			<view class="body">
				<view class="list">
					<view v-if="iswxflag==1" class="item" @click="to('/pages/m-pages/m-settle-account/index')">
						<view class="label">账户安全</view>
						<view class="top-infr-icon">
							<image class="img"
								src=""
								mode="aspectFill"></image>
						</view>
					</view>
					<view class="item" @tap="clearCacheConfirm">
						<view class="label">内存清理</view>
						<view style="position: absolute; right:40rpx; color:#fff;">{{cacheSize}}</view>
						<view class="top-infr-icon">
							<image class="img"
								src=""
								mode="aspectFill"></image>
						</view>
					</view>
					<view class="item">
						<view class="label">版本更新</view>
						<view style="position: absolute; right:40rpx; color:#fff;">V{{version}}</view>
						<view class="top-infr-icon">
							<image class="img"
								src=""
								mode="aspectFill"></image>
						</view>
					</view>
					<view v-if="iswxflag==1" class="item" @click="quit">
						<view class="label">退出登录</view>
					</view>
				</view>
				<!-- <view class="button" @click="quit">退出登录</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		to
	} from '@/utils/index.js';
	export default {
		data() {
			return {
				$c: this.$c,
				iswxflag: 1,
				heigh: 50,
				info: {},
				cacheSize: '',
				idNum: '',
				version: '',
				realName: '',
				title: '设置'
			}
		},
		onLoad(option) {
			// #ifdef APP-PLUS
			this.heigh = this.$store.state.StatusBar.statusBar;
			plus.runtime.getProperty(plus.runtime.appid, (info) => {
				this.version = info.version;
			});
			// #endif
		},
		onShow(option) {
			// #ifdef MP-WEIXIN
			this.iswxflag = 0;
			// #endif
			this.getInfo();
		},
		methods: {
			to,
			back() {
				to()
			},
			getAppInfo() {
				// #ifdef APP-PLUS
				let self = this;
				plus.cache.calculate(size => {
					if (size < 1024) {
						self.cacheSize = size + 'B';
					} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
						self.cacheSize = Math.floor((size / 1024) * 100) / 100 + 'KB';
					} else if (size / 1024 / 1024 >= 1) {
						self.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + 'M';
					}
				});
				// #endif
				// #ifdef H5
				let storageInfo = uni.getStorageInfoSync()
				console.log(storageInfo)
				this.cacheSize = storageInfo.currentSize.toString() + "KB";
				// #endif
			},
			clearCacheConfirm() {
				uni.showModal({
					title: "操作确认",
					content: "确认要清理缓存吗?",
					success: (res) => {
						if (res.confirm) {
							this.clearCache()
						}
					}
				})
			},
			//清除缓存
			clearCache() {
				let that = this;
				// #ifdef H5
				uni.clearStorageSync()
				uni.showToast({
					title: '缓存清理完成',
					icon: 'none',
				});
			},
			1000)
		// #endif

		// #ifdef APP-PLUS
		let os = plus.os.name;
		if (os == 'Android') {
			let main = plus.android.runtimeMainActivity();
			let sdRoot = main.getCacheDir();
			let files = plus.android.invoke(sdRoot, "listFiles");
			let len = files.length;
			for (let i = 0; i < len; i++) {
				let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
				plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
					if (entry.isDirectory) {
						entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  	
							that.getAppInfo(); // 重新计算缓存
						}, function(e) {
							console.log(e.message)
						});
					} else {
						entry.remove();
					}
				}, function(e) {
					console.log('文件路径读取失败')
				});
			}
			uni.showToast({
				title: '缓存清理完成',
				duration: 2000
			});

		} else { // ios  
			plus.cache.clear(function() {
				uni.showToast({
					title: '缓存清理完成',
					duration: 2000
				});
				that.getAppInfo();
			});
		}
		// #endif
	},
	quit() {
			uni.clearStorage();
			uni.reLaunch({
				url: '/im/login/login',
				success: () => {
					uni.clearStorage();
					uni.showToast({
						title: '退出成功',
						icon: 'none',
					});
					uni.$TUIKit.logout();
				}
			});
		},
		async getInfo() {
				this.getAppInfo()
			},
			async submit() {
				let res = await this.$c.post("/app/auth/realNameAuthIos?idNum=" + this.idNum + "&realName=" + this
					.realName)
				if (res.code != 0) {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
					return;
				}
				uni.showToast({
					title: "提交成功",
					icon: "none"
				})
				setTimeout(() => {
					// this.getInfo()
					this.$router.go(0)
				}, 1000)
			}
	}
	}
</script>

<style scoped lang="scss">
	.page {
		width: 100vw;
		padding: 30rpx;
		height: 100vh;
		background: linear-gradient(#e3ecfb, #fdedf0, #fff, #fff);
	}

	.top {
		position: fixed;
		top: 0rpx;
		width: 100%;
		// background-color: #e3ecfb;
		z-index: 999;
		text-align: center;
		vertical-align: middle;
		padding: 10rpx;
	}

	.top-icon {
		position: absolute;
		left: 30rpx;
		z-index: 99;
		width: 30rpx;
		height: 30rpx;
	}

	.top-title {
		line-height: 50rpx;
		display: block;
		height: 50rpx;
		z-index: 99;
		color: #fff;
	}

	.body {
		padding-top: 20rpx;
		text-align: center;

		.title {
			margin-top: 40rpx;
			font-size: 24rpx;
		}

		.top-infr-icon {
			width: 26rpx;
			height: 26rpx;
			position: absolute;
			right: 10rpx;
		}

		.button {
			background-color: #f70000;
			color: #fff;
			display: inline-block;
			padding: 30rpx 0rpx;
			transform: translateX(-30rpx);
			border-radius: 30rpx;
			font-size: 32rpx;
			width: 80%;
			margin-top: 30rpx;
		}

		.mark {
			width: 100%;
			margin-top: 30rpx;
			text-align: left;
			font-size: 20rpx;
			color: #9d9395;
		}

		.list {
			margin-top: 60rpx;

			.item {
				width: 90vw;
				height: 80rpx;
				line-height: 80rpx;
				margin: 30rpx;
				transform: translateX(-30rpx);
				position: relative;

				.label {
					position: absolute;
					left: 20rpx;
					color: #fff;
				}

				border-radius: 30rpx;
				background-color: #464143;
				padding:10rpx;
				// border-bottom: 1px solid #e2e2e2;
			}
		}
	}
</style>